﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace JasRibbonApplicatie.Views
{
    /// <summary>
    /// Interaction logic for Aanmelden.xaml
    /// </summary>
    public partial class AanmeldWindow : Window
    {
        private MainWindow mw;
        private Login login;
        private const int MINIMUM_CHARS = 2;
        private const string TITLE = "Jas Aanmelden";

        public AanmeldWindow(MainWindow mainw)
        {
            InitializeComponent();
            //set the screen in the middle at startup
            double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
            double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
            double windowWidth = this.Width;
            double windowHeight = this.Height;
            this.Left = (screenWidth / 2) - (windowWidth / 2);
            this.Top = (screenHeight / 2) - (windowHeight / 2);

            mw = mainw;
            login = Login.Instance;
            userTextbox.Focus();
        }

        private void loginButton_Click(object sender, RoutedEventArgs e)
        {
            if (userTextbox.Text == string.Empty)
                MessageBox.Show("Voer de gebruikersnaam in.", TITLE, MessageBoxButton.OK, MessageBoxImage.Information);
            else
            {
                login.getUserInfo(userTextbox.Text);

                if (login.User != null)
                {
                    if((passwordTextbox.Password != string.Empty) && (passwordTextbox.Opacity == 1))
                    {
                        if(login.CheckPassword(passwordTextbox.Password))
                        {
                            login.loadUserData();
                            login.isLoggedIn = true;
                            mw.Aanmelden();
                            this.Close();    
                        }
                        else
                        {
                            MessageBox.Show("Het wachtwoord is onjuist.", TITLE, MessageBoxButton.OK, MessageBoxImage.Information);
                            passwordTextbox.SelectAll();
                        }
                    }
                    else if((passwordTextbox.Password == string.Empty) && (passwordTextbox.Opacity == 1))
                    {
                        login.loadUserData();
                        login.isLoggedIn = true;
                        mw.Aanmelden();
                        this.Close();
                    }
                    else if((passwordTextbox.Opacity == 0))
                    {
                        login.loadUserData();
                        login.isLoggedIn = true;
                        mw.Aanmelden();
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("De gebruikersnaam bestaat niet of is geen magazijnierslid.", TITLE, MessageBoxButton.OK, MessageBoxImage.Information);
                    userTextbox.SelectAll();
                }   
            }
        }

        private void annulerenButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            if (!login.isLoggedIn) mw.Close();
        }

        // Summary:
        //      On change of usertextbox, we check if it is a administrator.
        //      If so, then a passwordbox will appear for extra functionality as an administrator
        //
        private void userTextbox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (userTextbox.Text.Length >= MINIMUM_CHARS)
            {
                login.getUserInfo(userTextbox.Text);
                if (login.User != null && login.User.rechten_id == 2)
                {
                    passwordTextbox.Visibility = Visibility.Visible;
                    passwordTextbox.Opacity = 1;
                }
                else
                {
                    passwordTextbox.Visibility = Visibility.Hidden;
                    passwordTextbox.Opacity = 0;
                }
            }
        }
    }
}
